package com.Shoping.controller;

import com.Shoping.entity.ShoppingList;
import com.Shoping.util.DbUtils;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 该实现类将会将购物车的内容以list的形式返回到前端
 * 具体实现方式，使用用户id来查购物车id，然后来查对应的商品块
 */
@WebServlet(value = "/shoppingList.do")
public class ShoppingListController extends HttpServlet {

    Connection connection = DbUtils.getConnection();
    ResultSet rs;
    PreparedStatement ps;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        Integer uid = Integer.valueOf(req.getParameter("uId"));//获取用户id
        Integer sid = 0;//购物车id

        //判断用户id是否为空，若为空，则需要注册
        if (uid == null) {
            /**
             * 此处要重定向到登录页
             */
            resp.sendRedirect("/ProgramJSP/register.jsp");
        } else {
            /**
             * 找到购物车id之后，去找商品块
             */
            List<ShoppingList> shoppingLists = new ArrayList<ShoppingList>();
            try {
                ps = connection.prepareStatement("select * from commoditys where s_id=" + uid);
                rs = ps.executeQuery();

                while (rs.next()) {
                    ShoppingList list = new ShoppingList();
                    list.setCmsId(rs.getInt("cms_id"));
                    list.setoNumber(rs.getString("o_number"));
                    list.setCmId(rs.getInt("cm_id"));
                    list.setsId(rs.getInt("s_id"));
                    list.setCmsNumber(rs.getInt("cms_number"));
                    list.setCmsSubtotal(rs.getDouble("cms_subtotal"));
                    list.setColor(rs.getString("cms_colorName"));

                    shoppingLists.add(list);
                }
            } catch (SQLException e) {
                e.printStackTrace();
                System.out.println("ShoppingListController---------------------------获取商品块id错误!");
            } finally {
                try {
                    ps.close();
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }


            for (int i = 0; i < shoppingLists.size(); i++) {
                //通过商品主键来查询商品价格和名字
                try {
                    ps = connection.prepareStatement("select cm_price,cm_name,cm_img from commodity where cm_id=" + shoppingLists.get(i).getCmId());
                    rs = ps.executeQuery();
                    if (rs.next()) {
                        shoppingLists.get(i).setCmName(rs.getString("cm_name"));
                        shoppingLists.get(i).setCmPrice(rs.getDouble("cm_price"));
                        shoppingLists.get(i).setCmImg(rs.getString("cm_img"));
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                    System.out.println("ShoppingListController---------------------------获取商品具体信息错误!");
                } finally {
                    try {
                        ps.close();
                        rs.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }

            Map<String, Object> map = new HashMap<>();
            map.put("list", shoppingLists);
            String list = JSON.toJSONString(map);
            resp.getWriter().write(list);
        }
    }
}
